home *** CD-ROM | disk | FTP | other *** search
/ Aminet 8 / Aminet 8 (1995)(GTI - Schatztruhe)[!][Oct 1995].iso / Aminet / dev / gcc / gcc270cp.lha / gnu / lib / g++-include / function.h < prev    next >
C/C++ Source or Header  |  1995-07-28  |  8KB  |  279 lines

  1. /*
  2.  *
  3.  * Copyright (c) 1994
  4.  * Hewlett-Packard Company
  5.  *
  6.  * Permission to use, copy, modify, distribute and sell this software
  7.  * and its documentation for any purpose is hereby granted without fee,
  8.  * provided that the above copyright notice appear in all copies and
  9.  * that both that copyright notice and this permission notice appear
  10.  * in supporting documentation.  Hewlett-Packard Company makes no
  11.  * representations about the suitability of this software for any
  12.  * purpose.  It is provided "as is" without express or implied warranty.
  13.  *
  14.  */
  15.  
  16. #ifndef FUNCTION_H
  17. #define FUNCTION_H
  18.  
  19. #include <bool.h>
  20.  
  21. template <class T>
  22. inline bool operator!=(const T& x, const T& y) {
  23.     return !(x == y);
  24. }
  25.  
  26. template <class T>
  27. inline bool operator>(const T& x, const T& y) {
  28.     return y < x;
  29. }
  30.  
  31. template <class T>
  32. inline bool operator<=(const T& x, const T& y) {
  33.     return !(y < x);
  34. }
  35.  
  36. template <class T>
  37. inline bool operator>=(const T& x, const T& y) {
  38.     return !(x < y);
  39. }
  40.  
  41. template <class Arg, class Result>
  42. struct unary_function {
  43.     typedef Arg argument_type;
  44.     typedef Result result_type;
  45. };
  46.  
  47. template <class Arg1, class Arg2, class Result>
  48. struct binary_function {
  49.     typedef Arg1 first_argument_type;
  50.     typedef Arg2 second_argument_type;
  51.     typedef Result result_type;
  52. };      
  53.  
  54. template <class T>
  55. struct plus : binary_function<T, T, T> {
  56.     T operator()(const T& x, const T& y) const { return x + y; }
  57. };
  58.  
  59. template <class T>
  60. struct minus : binary_function<T, T, T> {
  61.     T operator()(const T& x, const T& y) const { return x - y; }
  62. };
  63.  
  64. template <class T>
  65. struct times : binary_function<T, T, T> {
  66.     T operator()(const T& x, const T& y) const { return x * y; }
  67. };
  68.  
  69. template <class T>
  70. struct divides : binary_function<T, T, T> {
  71.     T operator()(const T& x, const T& y) const { return x / y; }
  72. };
  73.  
  74. template <class T>
  75. #ifdef __GNU__
  76. struct modulus {
  77.     typedef T first_argument_type;
  78.     typedef T second_argument_type;
  79.     typedef T result_type;
  80.     T operator()(const T& x, const T& y) const { return x % y; }
  81. };
  82. #else
  83. struct modulus : binary_function<T, T, T> {
  84.     T operator()(const T& x, const T& y) const { return x % y; }
  85. };
  86. #endif
  87.  
  88. template <class T>
  89. struct negate : unary_function<T, T> {
  90.     T operator()(const T& x) const { return -x; }
  91. };
  92.  
  93. template <class T>
  94. struct equal_to : binary_function<T, T, bool> {
  95.     bool operator()(const T& x, const T& y) const { return x == y; }
  96. };
  97.  
  98. template <class T>
  99. struct not_equal_to : binary_function<T, T, bool> {
  100.     bool operator()(const T& x, const T& y) const { return x != y; }
  101. };
  102.  
  103. template <class T>
  104. struct greater : binary_function<T, T, bool> {
  105.     bool operator()(const T& x, const T& y) const { return x > y; }
  106. };
  107.  
  108. template <class T>
  109. struct less : binary_function<T, T, bool> {
  110.     bool operator()(const T& x, const T& y) const { return x < y; }
  111. };
  112.  
  113. template <class T>
  114. struct greater_equal : binary_function<T, T, bool> {
  115.     bool operator()(const T& x, const T& y) const { return x >= y; }
  116. };
  117.  
  118. template <class T>
  119. struct less_equal : binary_function<T, T, bool> {
  120.     bool operator()(const T& x, const T& y) const { return x <= y; }
  121. };
  122.  
  123. template <class T>
  124. struct logical_and : binary_function<T, T, bool> {
  125.     bool operator()(const T& x, const T& y) const { return x && y; }
  126. };
  127.  
  128. template <class T>
  129. struct logical_or : binary_function<T, T, bool> {
  130.     bool operator()(const T& x, const T& y) const { return x || y; }
  131. };
  132.  
  133. template <class T>
  134. struct logical_not : unary_function<T, bool> {
  135.     bool operator()(const T& x) const { return !x; }
  136. };
  137.  
  138. template <class Predicate>
  139. class unary_negate : public unary_function<Predicate::argument_type, bool> {
  140. protected:
  141.     Predicate pred;
  142. public:
  143.     unary_negate(const Predicate& x) : pred(x) {}
  144.     bool operator()(const argument_type& x) const { return !pred(x); }
  145. };
  146.  
  147. template <class Predicate>
  148. unary_negate<Predicate> not1(const Predicate& pred) {
  149.     return unary_negate<Predicate>(pred);
  150. }
  151.  
  152. template <class Predicate> 
  153. class binary_negate 
  154.     : public binary_function<Predicate::first_argument_type,
  155.                  Predicate::second_argument_type, bool> {
  156. protected:
  157.     Predicate pred;
  158. public:
  159.     binary_negate(const Predicate& x) : pred(x) {}
  160.     bool operator()(const first_argument_type& x, 
  161.             const second_argument_type& y) const {
  162.     return !pred(x, y); 
  163.     }
  164. };
  165.  
  166. template <class Predicate>
  167. binary_negate<Predicate> not2(const Predicate& pred) {
  168.     return binary_negate<Predicate>(pred);
  169. }
  170.  
  171. template <class Operation> 
  172. class binder1st : public unary_function<Operation::second_argument_type,
  173.                     Operation::result_type> {
  174. protected:
  175.     Operation op;
  176.     Operation::first_argument_type value;
  177. public:
  178.     binder1st(const Operation& x, const Operation::first_argument_type& y)
  179.     : op(x), value(y) {}
  180.     result_type operator()(const argument_type& x) const {
  181.     return op(value, x); 
  182.     }
  183. };
  184.  
  185. template <class Operation, class T>
  186. binder1st<Operation> bind1st(const Operation& op, const T& x) {
  187.     return binder1st<Operation>(op, Operation::first_argument_type(x));
  188. }
  189.  
  190. template <class Operation> 
  191. class binder2nd : public unary_function<Operation::first_argument_type,
  192.                     Operation::result_type> {
  193. protected:
  194.     Operation op;
  195.     Operation::second_argument_type value;
  196. public:
  197.     binder2nd(const Operation& x, const Operation::second_argument_type& y) 
  198.     : op(x), value(y) {}
  199.     result_type operator()(const argument_type& x) const {
  200.     return op(x, value); 
  201.     }
  202. };
  203.  
  204. template <class Operation, class T>
  205. binder2nd<Operation> bind2nd(const Operation& op, const T& x) {
  206.     return binder2nd<Operation>(op, Operation::second_argument_type(x));
  207. }
  208.  
  209. template <class Operation1, class Operation2>
  210. class unary_compose : public unary_function<Operation2::argument_type,
  211.                                             Operation1::result_type> {
  212. protected:
  213.     Operation1 op1;
  214.     Operation2 op2;
  215. public:
  216.     unary_compose(const Operation1& x, const Operation2& y) : op1(x), op2(y) {}
  217.     result_type operator()(const argument_type& x) const {
  218.     return op1(op2(x));
  219.     }
  220. };
  221.  
  222. template <class Operation1, class Operation2>
  223. unary_compose<Operation1, Operation2> compose1(const Operation1& op1, 
  224.                            const Operation2& op2) {
  225.     return unary_compose<Operation1, Operation2>(op1, op2);
  226. }
  227.  
  228. template <class Operation1, class Operation2, class Operation3>
  229. class binary_compose : public unary_function<Operation2::argument_type,
  230.                                              Operation1::result_type> {
  231. protected:
  232.     Operation1 op1;
  233.     Operation2 op2;
  234.     Operation3 op3;
  235. public:
  236.     binary_compose(const Operation1& x, const Operation2& y, 
  237.            const Operation3& z) : op1(x), op2(y), op3(z) { }
  238.     result_type operator()(const argument_type& x) const {
  239.     return op1(op2(x), op3(x));
  240.     }
  241. };
  242.  
  243. template <class Operation1, class Operation2, class Operation3>
  244. binary_compose<Operation1, Operation2, Operation3> 
  245. compose2(const Operation1& op1, const Operation2& op2, const Operation3& op3) {
  246.     return binary_compose<Operation1, Operation2, Operation3>(op1, op2, op3);
  247. }
  248.  
  249. template <class Arg, class Result>
  250. class pointer_to_unary_function : public unary_function<Arg, Result> {
  251. protected:
  252.     Result (*ptr)(Arg);
  253. public:
  254.     pointer_to_unary_function(Result (*x)(Arg)) : ptr(x) {}
  255.     Result operator()(Arg x) const { return ptr(x); }
  256. };
  257.  
  258. template <class Arg, class Result>
  259. pointer_to_unary_function<Arg, Result> ptr_fun(Result (*x)(Arg)) {
  260.     return pointer_to_unary_function<Arg, Result>(x);
  261. }
  262.  
  263. template <class Arg1, class Arg2, class Result>
  264. class pointer_to_binary_function : public binary_function<Arg1, Arg2, Result> {
  265. protected:
  266.     Result (*ptr)(Arg1, Arg2);
  267. public:
  268.     pointer_to_binary_function(Result (*x)(Arg1, Arg2)) : ptr(x) {}
  269.     Result operator()(Arg1 x, Arg2 y) const { return ptr(x, y); }
  270. };
  271.  
  272. template <class Arg1, class Arg2, class Result>
  273. pointer_to_binary_function<Arg1, Arg2, Result> 
  274. ptr_fun(Result (*x)(Arg1, Arg2)) {
  275.     return pointer_to_binary_function<Arg1, Arg2, Result>(x);
  276. }
  277.  
  278. #endif
  279.